const localTime = require("../lib/localTime");
const ObjectId = require("mongoose");
const Chat = require('../models/index').getModel('chat')

const chatService = {
    async getChatMessage (query) {
        // console.log('getChatMessage', query)
        if (!query._id) {
            let result = await Chat.find().sort({ '_id': -1 }).limit(+query.limit)
            return result.reverse()
        } else {
            let result = await Chat.find({ '_id': { '$lt': query._id } }).sort({ '_id': -1 }).limit(+query.limit)
            return result.reverse()
        }
    },

    async insertOneChat (chat) {
        chat.sendTime = localTime()
        return Chat.create(chat)
    },
}

module.exports = chatService
